package com.wisdom.benefit.jufa.utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.wisdom.benefit.jufa.R;

public class DialogUtil {

    private Context mContext;
    private Activity mActivity;
    private OnItemClickListener mOnItemClickListener;

    public DialogUtil(Activity activity, Context context) {
        mActivity = activity;
        mContext = context;
    }

    /**
     * 定义一个接口，当用户点击按钮时，可在主页面进行逻辑操作
     */
    public interface OnItemClickListener {
        /**
         * 取消
         */
        void onItemCancelClick();

        /**
         * 确定
         */
        void onItemConfirmClick();

        /**
         * 带输入值确定
         */
        void onItemValueConfirmClick(String a);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    /**
     * 提示框,自定义标题，内容，按钮
     */
    public void dialog(String title, String content, String cancelString, String confirmString, String code) {
        LayoutInflater inflater = mActivity.getLayoutInflater();
        View view = inflater.inflate(R.layout.dialog_item, null);
        TextView titleView = view.findViewById(R.id.vi_title);
        TextView contentVIew = view.findViewById(R.id.vi_content);
        TextView cancelButtonView = view.findViewById(R.id.vi_cancel_button);
        TextView confirmButtonView = view.findViewById(R.id.vi_confirm_button);
        EditText editText = view.findViewById(R.id.et_content);
        if (code.equals("1")) {
            editText.setVisibility(View.GONE);
        } else {
            editText.setVisibility(View.VISIBLE);
        }
        titleView.setText(title);
        contentVIew.setText(content);
        cancelButtonView.setText(cancelString);
        confirmButtonView.setText(confirmString);

        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        final Dialog dialog = builder.create();
        dialog.setCancelable(false);
        dialog.show();
        dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE |
                WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
                }
            }
        });
        WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
        /** 设置宽为屏幕的0.8大小 */
        lp.width = (int) (mActivity.getWindowManager().getDefaultDisplay().getWidth() * 0.8);
        dialog.getWindow().setAttributes(lp);
        /** 自定义布局应该在这里添加，要在dialog.show()的后面 */
        dialog.getWindow().setContentView(view);
        cancelButtonView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickListener.onItemCancelClick();
                dialog.dismiss();
            }
        });
        confirmButtonView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickListener.onItemValueConfirmClick(editText.getText().toString());
                dialog.dismiss();
            }
        });
    }


    /**
     * 提示框,自定义标题，内容，按钮
     */
    public void newdialog(String content, String confirmString, String code) {
        LayoutInflater inflater = mActivity.getLayoutInflater();
        View view = inflater.inflate(R.layout.new_dialog_item, null);
        TextView contentVIew = view.findViewById(R.id.vi_content);
        TextView confirmButtonView = view.findViewById(R.id.vi_confirm_button);
        contentVIew.setText(content);
        confirmButtonView.setText(confirmString);
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        final Dialog dialog = builder.create();
        dialog.setCancelable(false);
        dialog.show();
        WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
        /** 设置宽为屏幕的0.8大小 */
        lp.width = (int) (mActivity.getWindowManager().getDefaultDisplay().getWidth() * 0.8);
        dialog.getWindow().setAttributes(lp);
        /** 自定义布局应该在这里添加，要在dialog.show()的后面 */
        dialog.getWindow().setContentView(view);

        confirmButtonView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickListener.onItemConfirmClick();
                dialog.dismiss();
            }
        });
    }
}

